import { defineStore } from "pinia";
import { ref, reactive } from "vue";
import message_notice from "../functional/message_notice";
import { useUrlStore } from './urlStore';
import { useRouter } from "vue-router";


// 答案的类型约束
interface answerType {
    answerId: number,
    answerContent: string,      //答案内容
    // right: string,
    // questionId: number
}

interface examCode {
    title: string,
    baseCode: string,
    describe: string,
    umpireCode: string,
}

// 题目的类型约束
interface subjectType {
    questionId: number,
    questionContent: string,     //题目内容
    questionType: string,
    examId: number,
    order: number,
    examAnswers: answerType[]
    examQuestionCode: examCode
}

export const useSubjects = defineStore("subjects", () => {
    let curSubjectIndex = ref<number>(0)     //表示当前题目的索引
    let subjetcLength = ref<number>(0)
    // 表示题目的详细信息
    const subjects = reactive({
        subjectData: [] as subjectType[]
    });

    const urlStore = useUrlStore()
    const router = useRouter()

    // 获取题目列表
    const getSubjects = async () => {
        try {
            let token: any = sessionStorage.getItem("token")
            let resp = await fetch(`${urlStore.baseurl}/exam/getExamQuestions`, {
                method: "GET",
                headers: {
                    "Content-Type": "application/json",
                    "token": token,
                }
            })
            let data = await resp.json()
            if (data.code === 200) {
                subjects.subjectData = data.data.records
                subjetcLength.value = subjects.subjectData.length
                message_notice("获取题目成功", "success")
            } else {
                message_notice("获取题目失败", "error")
                router.push("/index")
            }            
        } catch (error) {
            message_notice("获取题目失败", "error")
            
        }
    }

    return { curSubjectIndex, subjects, getSubjects, subjetcLength }
})
